package com.funmi.lizip.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.entity.InfraredAnimal;
import com.funmi.lizip.entity.dto.infraredAnimal.InfraredAnimalAddRequest;
import com.funmi.lizip.entity.dto.infraredAnimal.InfraredAnimalUpdateRequest;
import com.funmi.lizip.entity.dto.patrol.PatrolQueryRequest;
import com.funmi.lizip.entity.vo.PatrolQueryVo;
import com.funmi.lizip.service.InfraredAnimalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "动物")
@RestController
@RequestMapping("/infraredAnimalController")
public class InfraredAnimalController {

    @Autowired
    private InfraredAnimalService infraredAnimalService;

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "根据动物类型查询")
    @GetMapping("")
    public R get(String type){
        InfraredAnimal infraredAnimal = new InfraredAnimal();
        infraredAnimal.setType(type);
        QueryWrapper<InfraredAnimal> queryWrapper = new QueryWrapper<>(infraredAnimal);
        queryWrapper.orderByDesc("create_at");
        List<InfraredAnimal> list = infraredAnimalService.list(queryWrapper);
        return R.success().data(list);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "编辑")
    @PutMapping("")
    public R update(@RequestBody InfraredAnimalUpdateRequest request){
        InfraredAnimal infraredAnimal = new InfraredAnimal();
        BeanUtils.copyProperties(request, infraredAnimal);
        QueryWrapper<InfraredAnimal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",request.getId());
        boolean update = infraredAnimalService.update(infraredAnimal, queryWrapper);
        return R.success().data(update);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "新增")
    @PostMapping("")
    public R add(@RequestBody InfraredAnimalAddRequest request){
        InfraredAnimal infraredAnimal = new InfraredAnimal();
        BeanUtils.copyProperties(request, infraredAnimal);
        infraredAnimal.setCreateAt(LocalDateTime.now());
        InfraredAnimal queryAnimal = new InfraredAnimal();
        queryAnimal.setType(request.getType());
        QueryWrapper<InfraredAnimal> queryWrapper = new QueryWrapper<>(queryAnimal);
        List<InfraredAnimal> list = infraredAnimalService.list(queryWrapper);

        infraredAnimal.setSort(list.size());
        // infraredAnimal.setIsShow(true);
        boolean save = infraredAnimalService.save(infraredAnimal);
        return R.success().data(save);
    }





}
